<?php
/**
 * usermonitorpagedefine.php
 * Create on 2015-05-28
 * Create by yuebin
 */
namespace haibao\cms\business\behavior;

use \haibao\cms\data\behavior\UserMonitorPageDefine as UserMonitorPageDefineData;
use \haibao\cms\business\BusinessException;

class UserMonitorPageDefine extends \haibao\cms\business\Base{
	
	public static function searchRegionCode($code){
		$data = new UserMonitorPageDefineData();
		
		return $data->searchRegionCode($code);
	}
	
	public static function getRegionNames($pageId){
		$data = new UserMonitorPageDefineData();
	
		return $data->getRegionNames($pageId);
	}
	
	public static function AddUserMonitorPageDefine($model){
		self::checkOperation(self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_ADD);
		
		if(!isset($model->SiteId)){
			throw new BusinessException('站点不存在',self::DATA_EMPTY_ID);
		}
		if(!isset($model->MonitorName)){
			throw new BusinessException('名称不存在',self::DATA_EMPTY);
		}
		if(!isset($model->EventCode)){
			throw new BusinessException('事件列别不存在',self::DATA_EMPTY);
		}
		if(!isset($model->PageUrlRegex)){
			throw new BusinessException('URL正则地址不存在',self::DATA_EMPTY);
		}
		$model->Status = \haibao\cms\model\data\behavior\UserMonitorPageDefine::STATUS_ENABLE;
		$userMonitorPageDefineData = new UserMonitorPageDefineData();
		$userMonitorPageDefineData->add($model);
		
		self::saveOperateHistory(self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_ADD, $model->toArray(false));
	}
	
	public static function updateUserMonitorPageDefine($model){
		self::checkOperation(self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_EDIT);	
	
		if(!isset($model->SiteId)){
			throw new BusinessException('站点不存在',self::DATA_EMPTY_ID);
		}
		if(!isset($model->MonitorName)){
			throw new BusinessException('名称不存在',self::DATA_EMPTY);
		}
		if(!isset($model->EventCode)){
			throw new BusinessException('事件列别不存在',self::DATA_EMPTY);
		}
		if(!isset($model->PageUrlRegex)){
			throw new BusinessException('URL正则地址不存在',self::DATA_EMPTY);
		}

		$userMonitorPageDefineData = new UserMonitorPageDefineData();
		$userMonitorPageDefineData->updateById($model->Id, $model);
		
		self::saveOperateHistory(self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_EDIT, $model->toArray(false));
	}
	
	public static function getModelById($id){
		if(!$id){
			return NULL;
		}
		$userMonitorPageDefineData = new UserMonitorPageDefineData();
		return $userMonitorPageDefineData->getOneById($id);
	}
	
	public static function getUserMonitorPageList($filter){
		$userMonitorPageDefineData = new UserMonitorPageDefineData();
		return $userMonitorPageDefineData->getAll($filter);
	}
	
	
	/**
	 * 修改状态
	 * @param int $id
	 * @param int $status
	 */
	public static function changUserMonitorPageStatus($id,$status){
		if (!$id){
			throw new BusinessException('id不能为空',self::DATA_EMPTY_ID);
		}

		$userMonitorPageDefineData = new UserMonitorPageDefineData();
		$userMonitorPageDefineData->updateById($id, array(\haibao\cms\model\filter\behavior\UserMonitorPageDefine::CONDITION_FIELD_NAME_STATUS=>$status));
	
		self::saveOperateHistory(self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_EDIT, array('id'=>$id,'status'=>$status));
	}
	
	public static function getAllPageDefine($filter){
		$data = new UserMonitorPageDefineData();
		
		return $data->getAll($filter);
	}
}
